package com.hyfrogx.modules.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyfrogx.common.annotation.SysLog;
import com.hyfrogx.common.base.QueryForm;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.group.AddGroup;
import com.hyfrogx.common.validator.group.UpdateGroup;
import com.hyfrogx.modules.sys.entity.SysDictDetailEntity;
import com.hyfrogx.modules.sys.form.dict.detail.DictDetailSaveForm;
import com.hyfrogx.modules.sys.form.dict.detail.DictDetailUpdateForm;
import com.hyfrogx.modules.sys.form.dict.detail.DictDetailVo;
import com.hyfrogx.modules.sys.service.SysDictDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 字典详情 前端控制器
 * </p>
 *
 * @author wanglei
 * @date 2019-07-15 15:51:11
 */
@RestController
@RequestMapping("/sys/dict/detail")
@ApiIgnore
@Api(value = "字典详情controller", tags = {"sys--字典详情"})
public class SysDictDetailController extends AbstractController {

	@Autowired
	private SysDictDetailService dictDetailService;

	@ApiOperation("字典详情信息")
	@PostMapping("/info/{id}")
	@RequiresPermissions("sys:dict:detail:info")
	public R info(@PathVariable("id") Long id) {
		DictDetailVo vo = dictDetailService.queryVoById(id, DictDetailVo.class);
		return R.ok().put("data", vo);
	}

	@ApiOperation("字典详情列表")
	@PostMapping("/select")
	@RequiresPermissions("sys:dict:detail:select")
	public R select(@RequestBody QueryForm<SysDictDetailEntity> form) {
		//只查启用的
		form.setParams("status", 1);
		List<DictDetailVo> list = dictDetailService.queryVoList(
				dictDetailService.getSelectWrapper(form, true)
				, DictDetailVo.class
		);
		return R.ok().put("list", list);
	}

	@ApiOperation("字典详情列表(字典编码)")
	@PostMapping("/select/code/{dictCode}")
	public R selectByDictCode(@PathVariable String dictCode) {
		List<DictDetailVo> list = dictDetailService.selectByDictCode(dictCode);
		return R.ok().put("list", list);
	}

	@ApiOperation("字典详情列表(字典id)")
	@PostMapping("/select/id/{id}")
	public R selectByDictCode(@PathVariable Long dictId) {
		List<DictDetailVo> list = dictDetailService.selectByDictId(dictId);
		return R.ok().put("list", list);
	}


	@ApiOperation("字典详情列表")
	@PostMapping("/list")
	@RequiresPermissions("sys:dict:detail:list")
	public R list(@RequestBody QueryForm<SysDictDetailEntity> form) {
		IPage page = dictDetailService.queryVoPage(
				form.getIPage(),
				dictDetailService.getSelectWrapper(form, false)
				, DictDetailVo.class
		);
		return R.ok().put("page", page);
	}

	@ApiOperation("字典详情新建")
	@SysLog("保存字典详情")
	@PostMapping("/save")
	@RequiresPermissions("sys:dict:detail:save")
	public R save(@RequestBody @Validated(AddGroup.class) DictDetailSaveForm form) {
		if (ObjectUtil.isNotNull(dictDetailService.saveForm(getUserId(), form))) {
			return R.ok();
		}
		return R.error("保存字典详情失败");
	}

	@ApiOperation("字典详情更新")
	@SysLog("更新字典详情")
	@PostMapping("/update")
	@RequiresPermissions("sys:dict:detail:update")
	public R update(@RequestBody @Validated(UpdateGroup.class) DictDetailUpdateForm form) {
		if (ObjectUtil.isNotNull(dictDetailService.updateForm(getUserId(), form))) {
			return R.ok();
		}
		return R.error("修改字典详情失败");
	}

	@ApiOperation("字典详情启用")
	@SysLog("启用字典详情")
	@PostMapping("/enable")
	@RequiresPermissions("sys:dict:detail:enable")
	public R enable(@RequestBody List<Long> ids) {
		if (dictDetailService.enableByIdList(getUserId(), ids)) {
			return R.ok();
		}
		return R.error("启用字典详情失败");
	}

	@ApiOperation("字典详情禁用")
	@SysLog("禁用字典详情")
	@PostMapping("/disable")
	@RequiresPermissions("sys:dict:detail:disable")
	public R disable(@RequestBody List<Long> ids) {
		if (dictDetailService.disableByIdList(getUserId(), ids)) {
			return R.ok();
		}
		return R.error("禁用字典详情失败");
	}


	@ApiOperation("字典详情删除")
	@SysLog("删除字典详情")
	@PostMapping("/delete")
	@RequiresPermissions("sys:dict:detail:delete")
	public R delete(@RequestBody List<Long> ids) {
		if (dictDetailService.deleteByIdList(getUserId(), ids)) {
			return R.ok();
		}
		return R.error("删除字典详情失败");
	}
}

